<?php
// +----------------------------------------------------------------------+
// | Author:  Evgeny Leontev <eleontev@gmail.com>                         |
// | Copyright (c) 2005 Evgeny Leontev                                    |
// +----------------------------------------------------------------------+
// | This source file is free software; you can redistribute it and/or    |
// | modify it under the terms of the GNU Lesser General Public           |
// | License as published by the Free Software Foundation; either         |
// | version 2.1 of the License, or (at your option) any later version.   |
// |                                                                      |
// | This source file is distributed in the hope that it will be useful,  |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    |
// | Lesser General Public License for more details.                      |
// +----------------------------------------------------------------------+
//
// $Id: BoxMsg.php,v 1.0 2004/10/15 16:57:44 root Exp $

/**
 * BoxMsg is a set of classes to display messages.
 *
 * @since 15/10/2004
 * @author Evgeny Leontiev <eleontev@gmail.com>
 * @access public

 * EXAMPLE:
 * echo HintMsg::quickGet('Test');
 * 
 * $hint = new SuccessMsg();
 * $hint->setMsg('title','SUCCESS {url}');
 * $hint->setMsg('body','If your browser does not support automatic redirection 
 * 			          click this link <a h ref="{url}">{url}</a>');
 * $hint->assignVars('url', '555555');
 * echo $hint->get();
 */

require_once 'eleontev/Util/Replacer.php';
require_once 'eleontev/Util/GetMsg.php';


class BoxMsg
{	 

	var $error_pref = 'BoxMsg';
	var $vars = array();
	var $img_dir = '';
	var $img;	
	
	var $msg   = array('title'    =>'',
					   'body'     =>'',
					   'header'   =>''); 	
	
	var $color = array('title'    =>'#ffffff',
	                   'body'     =>'#000000'); 
	
	var $bgcolor = array('border' =>'#8592A2', 
					     'body'   =>'#FFFFE1', 
					     'title'  =>'#ffffff');
	

	function BoxMsg () {
		$this->replacer =& new Replacer();
	}	
	
	
	function factory($class, $msgs = array(), $vars = array()) {
		
		$class = ucfirst($class) . 'Msg';
		$m = new $class;
		$m->assignVars($vars);
		
		if($msgs) {
			if(!is_array($msgs)) {
				$args = func_get_args();
				unset($args[0]);
				$msgs = $m->getMsgsArrayFromArgs($args);
			} 
				
			$m->setMsgs($msgs);
			$m = &$m->get();
		}
		
		return $m;
	}
	
	
	function setMsg($msg_key, $msg) {
		$this->msg[$msg_key] = $msg;
	}	
	
	
/*
	function setMsgs1($msgs) {
		foreach($msgs as $k => $v) {
			$this->setMsg($k, $v);
		}
	}
*/
	
	
	function setMsgs($msgs) {
		
		if(!is_array($msgs)) { 	
			$msgs = $this->getMsgsArrayFromArgs(func_get_args());
		} 
		
		foreach($msgs as $k => $v) {
			$this->setMsg($k, $v);
		}		
	}
	
	
	function getMsgsArrayFromArgs($arr) {
		$msg_keys = array_keys($this->msg);
		foreach($arr as $k => $v) {
			$ar[current($msg_keys)] = $v;
			next($msg_keys);
		}

		return $ar;
	}
	
	
	
	function setMsgsIni($file, $msg_file_key) {
		return $this->setMsgs(GetMsg::parseIni($file, $msg_file_key));
	}
	
	
	function get() {
		foreach($this->msg as $k => $v) {
			if(!$v) { continue; }
			$this->msg[$k] = $this->replacer->parse($v);
		}
		
		return $this->getHtml();
	}	
	
	// DEPRECATED
	// echo SctreenMsg::quickGet(array of msg or str if only body);
	function quickGet($msgs = array()) { 
		$class = __CLASS__;
		$m = new $class;
		$m->setMsgs($msgs);
		return $m->getHtml();
	}
	
	
	function assignVars($var, $value = false) {
		$this->replacer->assignVars($var, $value);
	}
	
	
	function & getHtml() {
		$html = '<div class="boxmsg">' . "\n";
		$html .= ($this->msg['title']) ?  '<div class="boxmsg_title">'.$this->msg['title'].'</div>' : '';
		$html .= '<div>';
		$html .= ($this->img) ? '<img src="'.$this->img_dir . $this->img.'" alt="" style="margin: 0px 20px 10px 0px; float: left;" />' : '';
		$html .= ($this->msg['header']) ?  '<b>'.$this->msg['header'].'</b><br />' : '';
		$html .= $this->msg['body'];
	 	$html .= '</div>' . "\n";
		$html .= '</div>' . "\n";
		
		return $html;
	}
	
	
	/*
	function getHtml() {
		$tpl = new tplTemplatez($this->tmpl_dir . $this->tmpl);
		if($this->header_msg) { 
			$this->assign('header_msg', $this->header_msg);
			$this->assign('br', '<br>');
		}
		$tpl->tplAssign($this->vars);
		$tpl->tplParse(array('msg' => $this->msg));
		return $tpl->tplPrint(1);
	}
	*/
}


class HintMsg extends BoxMsg
{

	var $img = 'images/icons/hint.gif';
	
	var $msg   = array('title'   =>'',
					   'body'     =>'',
					   'header'   =>'');
	
	var $bgcolor = array('border' =>'#8592A2', 
					     'body'   =>'#FFFFE1', 
					     'title'  =>'#ffffff');
						 

	// DEPRECATED		   
	function quickGet($msgs = array()) { 
		$class = __CLASS__;
		$m = new $class;
		$m->setMsgs($msgs);
		return $m->getHtml();
	}	
}



class ErrorMsg extends BoxMsg
{
	
	var $img;
	
	var $msg   = array('title'   =>'ERROR',
					   'body'     =>'',
					   'header'   =>'');
	
	var $bgcolor = array('border' =>'#cc0033', 
					     'body'   =>'#FFFFE1', 
					     'title'  =>'#ffffff');
						 

	// DEPRECATED
	function quickGet($msgs = array()) { 
		$class = __CLASS__;
		$m = new $class;
		$m->setMsgs($msgs);
		return $m->getHtml();
	}
}



class SuccessMsg extends BoxMsg
{
	
	var $img;
	
	var $msg   = array('title'   =>'SUCCESS',
					   'body'     =>'',
					   'header'   =>'');

	var $bgcolor = array('border' =>'#339900', 
					     'body'   =>'#FFFFE1', 
					     'title'  =>'#ffffff');
						 

	// DEPRECATED
	function quickGet($msgs = array()) { 
		$class = __CLASS__;
		$m = new $class;
		$m->setMsgs($msgs);
		return $m->getHtml();
	}
}





/*
$msg = BoxMsg::factory('hint');
$msg->setMsgs('title', 21321);
echo $msg->get();

$msg = BoxMsg::factory('error');
$msg->setMsgs('title', 21321);
echo $msg->get();

$msg = BoxMsg::factory('success');
$msg->setMsgs('title', 21321);
echo $msg->get();
// or

$msg = new BoxMsg();
$msg->setMsgs('title', 21321);
echo $msg->get();
*/


//echo SuccessMsg::quickGet('title', 21321);


/*
$hint = new SuccessMsg();
$hint->setMsg('title','SUCCESS {url}');
$hint->setMsg('body','If your browser does not support automatic redirection 
			          click this link <a href="{url}">{url}</a>');
$hint->assignVars('url', '555555');
echo $hint->get();
*/

//echo BoxMsg::quickGet('Test');
?>